package com.fz.song.interceptor;


import com.fz.song.config.BlogConfig;
import com.fz.song.pojo.BlogAdmin;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    private static Log logger = LogFactory.getLog(LoginInterceptor.class);

    private long starttime = 0;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        starttime = System.currentTimeMillis();
        HttpSession session = request.getSession();
        BlogAdmin blogAdmin = (BlogAdmin) session.getAttribute(BlogConfig.ADMIN_LOGIN);

        if (blogAdmin != null) {
            return true;
        } else {
            response.sendRedirect(request.getContextPath() + "/admin/login.html");
            return false;
        }
    }



    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {

    }
}
